package com.huluobo.a2202monthdemo

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore.Video
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.huluobo.a2202monthdemo.adapter.CommonPagerAdapter
import com.huluobo.a2202monthdemo.databinding.ActivityMainBinding
import com.huluobo.a2202monthdemo.home.HomeFragment
import com.huluobo.a2202monthdemo.message.MessageFragment
import com.huluobo.a2202monthdemo.mine.MineFragment
import com.huluobo.a2202monthdemo.startLive.StartLiveActivity
import com.huluobo.a2202monthdemo.video.VideoFragment
import com.huluobo.base.view.BaseMVVMActivity

class MainActivity : BaseMVVMActivity<ActivityMainBinding>() {
    private lateinit var commonPagerAdapter: CommonPagerAdapter
    private val fragments = mutableListOf<Fragment>()
    override fun initData() {
        fragments.clear()
        fragments.add(HomeFragment())
        fragments.add(VideoFragment())
        fragments.add(MessageFragment())
        fragments.add(MineFragment())

        commonPagerAdapter = CommonPagerAdapter(this, fragments)
        dataBinding.mainVp2.adapter = commonPagerAdapter
    }

    override fun initView() {
        dataBinding.mainVp2.registerOnPageChangeCallback(object : OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                when (position) {
                    0 -> dataBinding.mainTab.menu.getItem(0).isChecked = true
                    1 -> dataBinding.mainTab.menu.getItem(1).isChecked = true
                    2 -> dataBinding.mainTab.menu.getItem(3).isChecked = true
                    3 -> dataBinding.mainTab.menu.getItem(4).isChecked = true
                }
            }
        })

        dataBinding.mainTab.setOnItemSelectedListener {
            when (it.itemId) {
                R.id.main_tab_home -> dataBinding.mainVp2.currentItem = 0
                R.id.main_tab_video -> dataBinding.mainVp2.currentItem = 1
                R.id.main_tab_start_live -> startActivity(Intent(this, StartLiveActivity::class.java))
                R.id.main_tab_message -> dataBinding.mainVp2.currentItem = 2
                R.id.main_tab_mine -> dataBinding.mainVp2.currentItem = 3
            }
            true
        }
    }

    override fun getLayoutId(): Int = R.layout.activity_main
}